﻿using System;
using System.Collections.Generic;

using Hammock.Web;

namespace TMWS
{
	using Models;

	/// <summary>
	/// Zone service
	/// </summary>
	public class ZoneService : ServiceBase
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="ZoneService" />
		/// </summary>
		/// <param name="username">API username</param>
		/// <param name="password">APU password</param>
		public ZoneService(string username, string password)
			: base(username, password, false)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ZoneService" />
		/// </summary>
		/// <param name="username">API username</param>
		/// <param name="password">APU password</param>
		/// <param name="enableLogging">Enable logging</param>
		/// <param name="logDirectory">Directory to store logs</param>
		public ZoneService(string username, string password, bool enableLogging, string logDirectory)
			: base(username, password, enableLogging, logDirectory)
		{
		}

		#endregion

		#region Public Methods

		/// <summary>
		/// Gets all the zones
		/// </summary>
		/// <param name="offset">Offset</param>
		/// <param name="length">Limit</param>
		/// <param name="sort">Sort</param>
		/// <param name="order">Order</param>
		/// <returns>Array of zones</returns>
		public List<TrackManiaZone> GetAllZones(int offset = 0, int length = 10, int order = 1, string sort = "")
		{
			string resource = String.Format("tmf/zones/all/?offset={0}&length={1}&order={2}{3}", offset, length, order, (!String.IsNullOrEmpty(sort) ? "&sort=" + sort : ""));

			return Execute<List<TrackManiaZone>>(resource, WebMethod.Get);
		}

		/// <summary>
		/// Gets the zone ID by the specified path
		/// </summary>
		/// <param name="path"></param>
		/// <returns></returns>
		public TrackManiaZone GetZoneIDByPath(string path)
		{
			string resource = String.Format("tmf/zones/path/{0}/id/", path);

			return Execute<TrackManiaZone>(resource, WebMethod.Get);
		}

		/// <summary>
		/// Gets information about the specified zone
		/// </summary>
		/// <param name="id">Zone ID</param>
		/// <returns>Zone information</returns>
		public TrackManiaZone GetZoneInformationByID(int id)
		{
			string resource = String.Format("tmf/zones/id/{0}/", id);

			return Execute<TrackManiaZone>(resource, WebMethod.Get);
		}

		/// <summary>
		/// Gets ifnroamtion the specified zone
		/// </summary>
		/// <param name="path">Zone path</param>
		/// <returns>Zone information</returns>
		public TrackManiaZone GetZoneInformationByPath(string path)
		{
			string resource = String.Format("tmf/zones/path/{0}/", path);

			return Execute<TrackManiaZone>(resource, WebMethod.Get);
		}

		/// <summary>
		/// Gets the children of the specified zone
		/// </summary>
		/// <param name="id">Zone ID</param>
		/// <param name="offset"></param>
		/// <param name="length"></param>
		/// <param name="order"></param>
		/// <param name="sort"></param>
		/// <returns>List of child zones</returns>
		public List<TrackManiaZone> GetZoneChildrenByID(int id, int offset, int length = 10, int order = 1, string sort = "")
		{
			string resource = String.Format("tmf/zones/id/{0}/?offset={1}&length={2}&order={3}{4}", id, offset, length, order, (!String.IsNullOrEmpty(sort) ? "&sort=" + sort : ""));

			return Execute<List<TrackManiaZone>>(resource, WebMethod.Get);
		}

		/// <summary>
		/// Gets the children of the specified path
		/// </summary>
		/// <param name="path"></param>
		/// <param name="offset"></param>
		/// <param name="length"></param>
		/// <param name="order"></param>
		/// <param name="sort"></param>
		/// <returns>List of child zones</returns>
		public List<TrackManiaZone> GetZoneChildrenByPath(string path, int offset, int length = 10, int order = 1, string sort = "")
		{
			string resource = String.Format("tmf/zones/path/{0}/?offset={1}&length={2}&order={3}{4}", path, offset, length, order, (!String.IsNullOrEmpty(sort) ? "&sort=" + sort : ""));

			return Execute<List<TrackManiaZone>>(resource, WebMethod.Get);
		}

		#endregion
	}
}